/* Wang Wen(Albert Wang) jawangwen@gmail.com */

#include <stdio.h>
#include <assert.h>
#include <product.h>
#include <product1.h>

#define RISK_FACTOR_SEED 0x8086
#define RISK_FACTOR_MAX  10
#define RISK_LEVEL_MAX   3

static const char * riskInfo[RISK_LEVEL_MAX] = {
    "No risk",
    "Low risk",
    "High risk",
};

static int riskFactor[RISK_FACTOR_MAX];

static void sendMessage (const char * str)
{
    printf("%s\n", str);
}

int addRisk (unsigned int riskValue)
{
    int indexRiskFactor;
    indexRiskFactor = riskValue % RISK_FACTOR_MAX;
    riskFactor[indexRiskFactor]++;
    return 0;
}

int subRisk (unsigned int riskValue)
{
    int indexRiskFactor;
    indexRiskFactor = riskValue % RISK_FACTOR_MAX;
    riskFactor[indexRiskFactor]--;
    return 0;
}

static int getRiskLevel (void)
{
    int indexRiskFactor;
    int riskLevel;

    riskLevel = RISK_FACTOR_SEED;
    for (indexRiskFactor = 0; indexRiskFactor < RISK_FACTOR_MAX; ++indexRiskFactor) {
        riskLevel >>= 1;
        riskLevel  ^= riskFactor[indexRiskFactor];
    }
    riskLevel &= RISK_LEVEL_MAX;

    return riskLevel;
}

void showRisk (void)
{
    int riskLevel;

    riskLevel = getRiskLevel();
    assert(riskLevel >=0 && riskLevel < RISK_LEVEL_MAX);

    sendMessage(riskInfo[riskLevel]);
}

